package asia.doing.controller.manage;


import asia.doing.common.Result;
import asia.doing.entity.Tool;
import asia.doing.service.manage.ToolService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "工具管理" ,description = "数据管理页面工具接口管理")
@RestController(value = "manageToolController")
@RequestMapping("/v1/manage/tool")
public class ToolController {
    @Resource
    private ToolService toolService;


    @ApiOperation(value="id查询工具")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Tool tool = new Tool();
        tool.setId(id);
        List<Tool> tools = toolService.selectTool(tool);
        return Result.success(tools.get(0));
    }


    @ApiOperation(value = "添加工具")
    @PostMapping
    public Result addTool(@RequestBody Tool tool){
        toolService.addTool(tool);
        return Result.success();
    }


    @ApiOperation(value ="更新工具")
    @PutMapping
    public Result updateTool(@RequestBody Tool tool){
        toolService.updateTool(tool);
        return Result.success();
    }


    @ApiOperation(value = "删除工具")
    @DeleteMapping("/{id}")
    public Result deleteTool(@PathVariable Integer id){
        toolService.deleteTool(id);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @ApiOperation(value="工具分页")
    @GetMapping
    public Result selectPage(Tool tool,
                             @RequestParam(required = true) Integer pageNum,
                             @RequestParam(required = true) Integer pageSize){
        //查询所有的工具信息
        if(pageNum == null || pageSize == null){
            List<Tool> tools = toolService.selectTool(tool);
            return Result.success(tools);
        }else{
            PageInfo<Tool> page = toolService.selectPage(tool, pageNum, pageSize);
            return Result.success(page);
        }

    }
}
